package com.honghu.cloud.service.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.honghu.cloud.bean.Goods;
import com.honghu.cloud.bean.GoodsSpecProperty;
import com.honghu.cloud.common.page.Page;
import com.honghu.cloud.common.service.BaseService;
import com.honghu.cloud.dao.GoodsDao;
import com.honghu.cloud.dto.AccessoryDto;
import com.honghu.cloud.service.IGoodsService;

/**
 * service实现类
 * @author Administrator
 *
 */
@Service
@Transactional(readOnly = true)
public class GoodsServiceImpl extends BaseService<Goods> implements IGoodsService {
	
	@Autowired
	private GoodsDao goodsDao;
	

	@Transactional(readOnly = false)
	public void batchDelObjs(List<Goods> objs) {
		if (objs != null && objs.size() > 0) {
			goodsDao.batchDelete(objs);
		}
	}


	public Goods getObjByProperty(String key, String operation_symbol, Object value) {
		Map<String, Object> maps = Maps.newHashMap();
		maps.put("operation_property", key);
		maps.put("operation_symbol", operation_symbol);
		maps.put("operation_value", value);
		List<Goods> objs = goodsDao.selectObjByProperty(maps);
		if (objs != null && objs.size() > 0) {
			return objs.get(0);
		}
		return null;
	}

	public List<Goods> selectObjByProperty(Map<String, Object> maps) {
		return goodsDao.selectObjByProperty(maps);
	}

	public List<Goods> queryPages(Map<String, Object> params) {
		return goodsDao.queryPages(params);
	}

	public List<Goods> queryPageListWithNoRelations(Map<String, Object> param) {
		return goodsDao.queryPageListWithNoRelations(param);
	}


	public List<Goods> queryPagesWithNoRelations(Map<String, Object> params, Integer currentPage, Integer pageSize) {

		if (currentPage != null) {
			params.put("currentPage", currentPage);
		}

		if (pageSize != null) {
			params.put("pageSize", pageSize);
		}

		return goodsDao.queryPagesWithNoRelations(params);
	}

	public List<Goods> queryPagesWithNoRelations2(Map<String, Object> params) {

		return goodsDao.queryPagesWithNoRelations2(params);
	}

	public Page<Goods> queryPagesWithNoRelations(Map<String, Object> params) {
		return super.queryPagesWithNoRelations(params);
	}


	public Page<Goods> list(Map<String, Object> params) {
		return super.list(params);
	}


	@Transactional(readOnly = false)
	public void batchDeleteByIds(List<Long> ids) {
		goodsDao.batchDeleteByIds(ids);
	}


	@Transactional(readOnly = false)
	public void saveEntity(Goods obj) {
		goodsDao.saveEntity(obj);
	}


	@Transactional(readOnly = false)
	public void updateById(Goods obj) {
		goodsDao.updateById(obj);
	}


	@Transactional(readOnly = false)
	public void deleteById(Long id) {
		goodsDao.deleteById(id);
	}


	public Goods selectByPrimaryKey(Long id) {
		return goodsDao.selectByPrimaryKey(id);
	}

	public List<Goods> queryPageList(Map<String, Object> params, Integer begin, Integer max) {
		return super.queryPageList(params, begin, max);
	}

	public List<Goods> queryPageList(Map<String, Object> params) {
		return this.queryPageList(params, null, null);
	}


	public int selectCount(Map<String, Object> params) {
		Integer c = goodsDao.selectCount(params);
		if (c == null) {
			return 0;
		}

		return c;
	}

	@Transactional(readOnly = false)
	public void batchDeleteGoodsPhotos(Long goodsId, List<AccessoryDto> goodsPhotos) {
		Map<String, Object> params = Maps.newHashMap();
		params.put("goodsId", goodsId);
		params.put("goodsPhotos", goodsPhotos);
		if (goodsId != null && goodsPhotos != null && goodsPhotos.size() > 0) {
			goodsDao.batchDeleteGoodsPhotos(params);
		}
	}



	@Transactional(readOnly = false)
	public void batchDeleteGoodsSpecProperty(Long goodsId, List<GoodsSpecProperty> gsps) {
		Map<String, Object> params = Maps.newHashMap();
		params.put("goodsId", goodsId);
		params.put("gsps", gsps);
		if (goodsId != null && gsps != null && gsps.size() > 0) {
			goodsDao.batchDeleteGoodsSpecProperty(params);
		}
	}

	@Transactional(readOnly = false)

	public void deleteGoodsMainPhoto(Long id) {
		goodsDao.deleteGoodsMainPhoto(id);
	}


	@Transactional(readOnly = false)
	public void delete(Long id) {
		goodsDao.delete(id);
	}


	@Transactional(readOnly = false)
	public void update(Goods obj) {
		goodsDao.update(obj);
	}


	public List<Goods> queryGoodsByGoodsClassIds(Map<String, Object> params) {
		List<Goods> lists = Lists.newArrayList();
		if (params != null && params.size() > 0) {
			lists = goodsDao.queryGoodsByGoodsClassIds(params);
		}

		return lists;
	}

	@Transactional(readOnly = false)

	public void removeGoodsBrandByGoodsBrandId(Map<String, Object> params) {
		goodsDao.removeGoodsBrandByGoodsBrandId(params);
	}

	@Transactional(readOnly = false)
	public void saveGoodsPhotos(Long id, List<AccessoryDto> goods_photos) {
		Map<String, Object> params = Maps.newHashMap();
		params.put("goods_id", id);
		params.put("goods_photos", goods_photos);
		if (goods_photos != null && goods_photos.size() > 0) {
			goodsDao.batchInsertGoodsPhotos(params);
		}
	}

	@Transactional(readOnly = false)
	public void saveGoodsSpecProperty(Long id, List<GoodsSpecProperty> gsps) {
		Map<String, Object> params = Maps.newHashMap();
		params.put("goods_id", id);
		params.put("gsps", gsps);
		if (gsps != null && gsps.size() > 0) {
			goodsDao.batchInsertGoodsSpecs(params);
		}
	}
	
	@Transactional(readOnly = false)
	public void saveGoodsSpecProperty2(Long id, List<GoodsSpecProperty> gsps) {
		Map<String, Object> params = Maps.newHashMap();
		params.put("goods_id", id);
		params.put("gsps", gsps);
		if (gsps != null && gsps.size() > 0) {
			goodsDao.batchInsertGoodsSpecs2(params);
		}
	}
	


	public List<Map<String, Object>> queryGoodsList(Map<String, Object> params) {
		params.put("deleteStatus", "0");
		return goodsDao.queryGoodsList(params);
		
	}


	public Map<String, Object> selectGoodsById(Long id) {
		return goodsDao.selectGoodsById(id);
	}


	@Override
	@Transactional(readOnly = false)
	public void addUserLiveGoods(Map<String, Object> params) {
		 goodsDao.addUserLiveGoods(params);
	}

	@Override
	@Transactional(readOnly = false)
	public void addStoreAgentGoods(Map<String, Object> params) {
		goodsDao.addStoreAgentGoods(params);
	}
	
	@Override
	@Transactional(readOnly = false)
	public void delUserLiveGoods(Map<String, Object> params) {
		goodsDao.delUserLiveGoods(params);
	}

	@Override
	@Transactional(readOnly = false)
	public void delStoreAgentGoods(Map<String, Object> params) {
		goodsDao.delStoreAgentGoods(params);
	}
	
	public Integer findUserLiveGoods(Long uid, Long goods_id){
		return goodsDao.findUserLiveGoods(uid, goods_id);
	}
	
	public Integer findStoreAgentGoods(Long store_id, Long goods_id){
		return goodsDao.findStoreAgentGoods(store_id, goods_id);
	}


	@Override
	public Map<String, Object> goodsStoreStatis(Long store_id) {
		return goodsDao.goodsStoreStatis(store_id);
	}
	
	public List<Goods> getRecommendGoods(List<Long> ids){
		return goodsDao.getRecommendGoods(ids);
	}


	@Override
	@Transactional(readOnly = false)
	public void batchDeleteUserGoodsClass(Map<String, Object> params) {
		// TODO Auto-generated method stub
		goodsDao.batchDeleteUserGoodsClass(params);
	}


	@Override
	@Transactional(readOnly = false)
	public void batchDeleteGoodsPhotos(Map<String, Object> params) {
		// TODO Auto-generated method stub
		goodsDao.batchDeleteGoodsPhotos(params);
	}


	@Override
	@Transactional(readOnly = false)
	public void batchDeleteGoodsSpecProperty(Map<String, Object> params) {
		// TODO Auto-generated method stub
		goodsDao.batchDeleteGoodsSpecProperty(params);
	}






	@Override
	@Transactional(readOnly = false)
	public void deleteGoodsPhotosById(Long id) {
		goodsDao.deleteGoodsPhotosById(id);
		
	}



	@Override
	public int selectSalenum(Map<String, Object> params) {
		Integer c = goodsDao.selectSalenum(params);
		if (c == null) {
			return 0;
		}

		return c;
	}


	






	private int offLineCount(Map<String, Object> params) {
		Integer c = goodsDao.offLineCount(params);
		if (c == null) {
			return 0;
		}
		return c;
	}



	private int goTravelCount(Map<String, Object> params) {
		Integer c = goodsDao.goTravelCount(params);
		if (c == null) {
			return 0;
		}
		return c;
	}
	
	
	
	

}
